package com.wx.cloud.security.controller;


import com.wx.cloud.security.common.CommonResult;
import com.wx.cloud.security.model.LoginParams;
import com.wx.cloud.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;

@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${jwt.tokenHead}")
    String tokenHead;

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginParams loginParams){

        HashMap<String, String> data = new HashMap<>();
        String token = null;
        try {
            token = userService.login(loginParams);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.validateFailed("用户名或密码错误");
        }
        if (StringUtils.isEmpty(token)){
            return CommonResult.validateFailed("用户名或密码错误");
        }
        data.put("tokenHead",tokenHead);
        data.put("access_token",token);
        // localStorage.setItem("Authorization","Bearer sdsdfdfds")
        // $ajax{data:{},type:"",header:{"Authorization":"Bearer sdsdfdfds"}}
        return CommonResult.success(data);
    }

//    @PreAuthorize("hasAuthority('wx:product:read')")
//    @PostMapping("/login2")
//    public CommonResult login(Authentication authentication){
//        return CommonResult.success(authentication);
//    }

    //todo  注册
    //1 接受账号密码
    //2 查询是有存在重复账号
    //3 密码加密
    //4 保存到数据库
}

